<template>
  <div class="app-container">
    <el-form ref="placeCodeForm" v-loading="formLoading" :rules="rules" :model="form" label-position="top">
      <el-form-item label="专业" prop="name">
        <el-input placeholder="请填写专业" v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="行业" prop="industry">
        <el-cascader v-model="form.industry" placeholder="请选择行业"
                     :options="jobList"
                     :props="optionProps"
                     @change = "change"
                     clearable
        ></el-cascader>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  editSpecialityApi, getJobListApi, insertAndUpdateSpeciality,
} from '@/api/trainCode'

export default {
  name: 'PlaceCodeDetail',
  data() {
    return {
      editor: null,
      //期望职位级联选择器配置
      optionProps: {
        value: 'id',
        label: 'jobTitle',
        children: 'subLevelModelList',
        multiple: true,
        emitPath:false
      },
      jobList: [],
      toolbarConfig: {
        excludeKeys: [
          // 排除菜单组，写菜单组 key 的值即可
          'group-video' // 去掉视频
        ]
      },
      editorConfig: {MENU_CONF: {}, placeholder: ''},
      mode: 'default',
      uploadUrl: process.env.VUE_APP_UPLOAD_API,
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        id: null,
        name: '',
        industry: '',
      },
      rules: {
        name: [{required: true, trigger: 'blur', message: '请输入专业名称'}],
        industry: [{required: true, trigger: 'blur', message: '请选择行业'}]
      },
      formLoading: false,
      arr:[]
    }
  },
  props: ['id', 'closeDialog','trainId','diaList','type'],
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    change(e)
    {
      console.log(e)
      console.log(this.form.industry)
    },
    getDetail(){
      if(this.diaList){
        this.form.name=this.diaList.name
        this.form.id=this.diaList.id
        console.log(this.diaList.industry)
        this.form.industry=this.diaList.industry.split(',')
        this.form.industry.forEach(item=>{
          Number(item)
          this.arr.push( Number(item))
        })
        for (let i = 0; i < this.arr.length; i++) {
          this.form.industry[i] = this.arr[i]
        }

        console.log(this.form.industry)
      }
    },
    submit() {
      this.$refs.placeCodeForm.validate(valid => {
        if (valid) {
          const loading = this.$loading({
            text: '提交中'
          })
          if (this.form.id == null) {
            this.form.id = 0;
          }
          const form = JSON.parse(JSON.stringify(this.form));
          // form.industry.forEach(item=>{
          //   this.arr.push(item[2])
          // })
          form.industry=form.industry.join(',')
          console.log(form.industry)
          insertAndUpdateSpeciality(form).then((res) => {
            this.$message.success('保存成功');
            this.closeDialog(0);
          }).catch(() => {
            this.$message.error('保存失败');
          }).finally(() => {
            loading.close();
          })
        } else {
          return false
        }
      })
    },
    getTypeList()
    {
      getJobListApi().then((res) => {
        this.jobList = res;
      }).finally(() => {
        this.formLoading = false;
      })
    },
  },
  mounted() {
    this.getTypeList()
    if (this.type==2) {
      this.getDetail()
    }
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  },
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #c5c5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

}
</style>
<style scoped>
/*
*让select撑开
*/
.el-select, .el-cascader {
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed #c5c5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
